import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * https://gitee.com/li--jiaqiang/java-test.git
 * git pull
 * git pull origin master
 * git pull origin master --allow-unrelated-histories
 * User: 86187
 * Date: 2022-04-27
 * Time: 12:31
 */

//Scanner scan = new Scanner(System.in);

public class MyArrayList {

    public int[] elem; //数组
    public int useSize; //有效数据个数
    public static final int intCapacity = 5; //初始容量

    public MyArrayList() {
        this.elem = new int[intCapacity];
        this.useSize = 0;
    }

    //打印顺序表
    public void disPaly() {
        for (int i = 0; i < this.useSize; i++) {
            System.out.print(this.elem[i]+" ");
        }
        System.out.println();
    }

    //判断是否需要需要扩容
    private void newCapacity() {
        if(this.useSize == this.elem.length) {
            this.elem = Arrays.copyOf(elem, elem.length * 2);
        }
    }

    //新增元素
    public void add(int data) {
        newCapacity(); // //判断是否需要需要扩容
        elem[this.useSize] = data;
        this.useSize++;
    }

    //在pos位置插入数据
    public void addPos(int pos,int data) {
        newCapacity(); // //判断是否需要需要扩容

        if(pos < 0 && pos > this.useSize) {
            System.out.println("输入的pos："+pos+"无效！添加失败！");
            return;
        }

        for(int i = this.useSize - 1; i >= pos; i--) {
            this.elem[i+1] = this.elem[i];
        }
        this.elem[pos] = data;
        this.useSize++;
    }

    //判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < this.useSize; i++) {
            if(this.elem[i] == toFind) {
                 return true;
            }
        }
        return false;
    }

    //查找某个元素对应的位置
    public int search(int toFind) {
        for (int i = 0; i < this.useSize; i++) {
            if(this.elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    //获取pos位置的元素
    public int getPos(int pos) {
        if(this.useSize == 0) {
            System.out.println("顺序表已为空！");
            return -1;
        }

        if(pos >= 0 && pos < this.useSize) {
            return this.elem[pos];
        }
        System.out.println("输入的pos："+pos+"无效！获取失败！");
        return -1;
    }

    //给pos位置的元素设为 value
    public void setPos(int pos,int value) {
        if(pos < 0 && pos > this.useSize) {
            System.out.println("输入的pos："+pos+"无效！设置失败！");
            return;
        }
        this.elem[pos] = value;
    }

    //删除第一次出现的关键字key
    public void remove(int toMove) {
        if(this.useSize == 0) {
            return;
        }
        boolean flg = false;
        int pos = 0;
        for (int i = 0; i < this.useSize; i++) {
            if(this.elem[i] == toMove) {
                pos = i;
                flg = true;
                break;
            }
        }
        if(flg) {
            for (int i = pos+1; i < this.useSize; i++) {
                this.elem[i-1]=this.elem[i];
            }
            this.useSize--;
        }
    }

    //删除pos位置的数据
    public void deletePos(int pos) {
        if(this.useSize == 0) { //为空直接退出
            return;
        }

        if(pos < 0 && pos > this.useSize) {
            System.out.println("输入的pos：" + pos + "无效！删除失败！");
            return;
        }
        for (int i = pos+1; i < this.useSize; i++) {
            this.elem[i-1] = this.elem[i];
        }
        this.elem[this.useSize-1] = 0;
        this.useSize--;
    }

    //获取顺序表长度
    public int size(){
        return this.useSize;
    }

    //清空顺序表
    public void destroy() {
        for (int i = useSize - 1; i >= 0 ; i--) {
            this.elem[i] = 0;
            this.useSize--;
        }
    }
}
